#include "Socket.hpp"
#include "Protocol.hpp"
using namespace SocketModule;

void Usage(const std::string &name)
{
    std::cout << "usage: " << name << " + ip" << " + port" << std::endl;
}

int main(int argc, char* args[])
{
    if (argc != 3)
    {
        Usage(args[0]);
        exit(USAGE_ERROR);
    }

    in_port_t port = std::stoi(args[2]);
    TCPClientSocket cli_socket(args[1], port);

    int x, y;
    char oper;
    Request request;
    Response response;
    Protocol protocol([](const Request&){return Response();});
    std::string send_msg, recv_msg, json_str;
    InetAddr server = cli_socket.addr();
    while(true)
    {
        std::cout << "Enter expression(x oper y)# ";
        std::cin >> x >> oper >> y;
        request.serialize(x, y, oper);
        protocol.encapsulate(request.JsonStr(), send_msg);
        cli_socket.Send(send_msg);

        std::string tmp;
        while(!protocol.decapsulate(recv_msg, json_str))
        {
            cli_socket.Receive(tmp);
            recv_msg += tmp;
        }
        response.deserialize(json_str);
        std::cout << "Recive from Client# " << response.Result() << "[" << response.Status() << "]" << std::endl;
    }

    return 0;
}